package com.sita.tboard.hitchhike.search;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;
import com.sita.bike.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaodong on 16/3/29.
 */
public class PoiKeywordSearchResultAdapter extends RecyclerView.Adapter<PoiKeywordSearchResultAdapter.PoiKeywordSearchResultViewHolder>{
    private ArrayList<PoiItem> poiItems = new ArrayList<PoiItem>();

    private Context context;
    private LayoutInflater layoutInflater;

    @Override
    public PoiKeywordSearchResultViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null){
            context = parent.getContext();
            layoutInflater = LayoutInflater.from(context);
        }
        View view = layoutInflater.inflate(R.layout.item_poi_keyword_search_in_list, parent, false);
        PoiKeywordSearchResultViewHolder viewHolder = new PoiKeywordSearchResultViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(PoiKeywordSearchResultViewHolder holder, int position) {
        if (position < 0 || position > getItemCount()){
            return;
        }
        final PoiItem poiItem = getItem(position);
        holder.addressView.setText(poiItem.getTitle());
    }

    @Override
    public int getItemCount() {
        return poiItems.size();
    }

    public PoiItem getItem(int position){
        return poiItems.get(position);
    }

    public void addItems(List<PoiItem> poiItems1){
        int curSize = getItemCount();
        poiItems.addAll(poiItems1);
        notifyItemRangeChanged(curSize, getItemCount());
    }

    static class PoiKeywordSearchResultViewHolder extends RecyclerView.ViewHolder{
        TextView addressView;

        public PoiKeywordSearchResultViewHolder(View itemView) {
            super(itemView);

            addressView = (TextView)itemView.findViewById(R.id.poi_address);
        }
    }
}
